package com.newmodelclient.baselib.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.newmodelclient.baselib.R;
import com.newmodelclient.baselib.enums.FCPageViewState;


/**
 * 文件名：FCPageStateView
 * 描 述：默认状态页
 * 作 者：gyq
 * 时 间：2021-1-22
 */
public class FCPageStateView extends FrameLayout {

    private LinearLayout mRootView;
    private ImageView mStateImageView;
    private TextView mStateTextView;
    private TextView mStateRefreshView;
    private TextView mStateGuidView;
    private @FCPageViewState
    int viewState;
    private FCPageStateClickListener mFCPageStateClickListener;

    public void setFCPageStateClickListener(FCPageStateClickListener mFCPageStateClickListener) {
        this.mFCPageStateClickListener = mFCPageStateClickListener;
    }

    public FCPageStateView(@NonNull Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.view_base_state, this);
        initUI();
    }

    public FCPageStateView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_base_state, this);
        initUI();
    }

    private void initUI() {
        mRootView = findViewById(R.id.ll_base_state);
        mStateImageView = findViewById(R.id.img_base_state);
        mStateTextView = findViewById(R.id.tv_base_state);
        mStateRefreshView = findViewById(R.id.tv_base_state_refresh);
        mStateGuidView = findViewById(R.id.tv_base_guide);

        SpannableString textSpanned = new SpannableString("无法连接网络，查看解决方案");
        textSpanned.setSpan(new UnderlineSpan(),
                7, textSpanned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpanned.setSpan(new ForegroundColorSpan(Color.parseColor("#FF397BE6")),
                7, textSpanned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mStateGuidView.setText(textSpanned);

        setViewState(FCPageViewState.PAGE_STATE_NORMAL, "");
        mRootView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewState == FCPageViewState.PAGE_STATE_NET_ERROR) {
                    if (mFCPageStateClickListener != null) {
                        mFCPageStateClickListener.onClickPageStateErrorView();
                    }
                }
            }
        });

        mStateGuidView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getContext().startActivity(new Intent(getContext(), FCPageWebFailedActivity.class));
            }
        });
    }

    public void setViewState(@FCPageViewState int viewState) {
        setViewState(viewState, -1, null);
    }

    public void setViewState(@FCPageViewState int viewState, String msg) {
        setViewState(viewState, -1, msg);
    }

    public void setViewState(@FCPageViewState int viewState, int img, String msg) {
        this.viewState = viewState;
        switch (viewState) {
            case FCPageViewState.PAGE_STATE_NORMAL: {
                mStateGuidView.setVisibility(GONE);
                setVisibility(GONE);
                if (getParent() != null) {
                    ((ViewGroup) getParent()).removeView(this);
                }
            }
            break;
            case FCPageViewState.PAGE_STATE_NO_DATA: {
                setVisibility(VISIBLE);
                mStateGuidView.setVisibility(GONE);
                mStateRefreshView.setVisibility(GONE);
                if (img != -1) {
                    mStateImageView.setImageResource(img);
                } else {
                    mStateImageView.setImageResource(R.mipmap.base_state_empty);
                }
                if (msg != null) {
                    mStateTextView.setText(msg);
                } else {
                    mStateTextView.setText(getContext().getString(R.string.base_state_empty_text));
                }
            }
            break;
            case FCPageViewState.PAGE_STATE_NET_ERROR: {
                setVisibility(VISIBLE);
                mStateGuidView.setVisibility(GONE);
                mStateRefreshView.setVisibility(VISIBLE);
                if (img != -1) {
                    mStateImageView.setImageResource(img);
                } else {
                    mStateImageView.setImageResource(R.mipmap.base_state_error);
                }
                if (msg != null) {
                    mStateTextView.setText(msg);
                } else {
                    mStateTextView.setText(getContext().getString(R.string.base_state_error_text));
                }
            }
            break;
        }
    }

    /**
     * 设置局中顶部的偏移量
     */
    private void setTopOffsetByPx(int topOffset) {
        mRootView.setPadding(0, topOffset, 0, 0);
    }


}
